package com.bsj.travel.process.handle.service;

import com.bsj.consumerQ.method.QueueAndRun;
import com.bsj.travel.process.constants.DataHandleConstant;
import com.bsj.travel.process.handle.RunAllService;
import com.bsj.travel.process.handle.service.impl.DefaultTdEngineHandleRun;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yinhao
 * @version 1.0
 * @description 时序数据存储
 * @date 2024/2/18
 */
@Service("defaultTdEngineService")
@Slf4j
public class DefaultTdEngineService implements RunAllService {

    @Autowired
    private DefaultTdEngineHandleRun defaultTdEngineHandleRun;

    private QueueAndRun queueAndRun = null;

    /**
     * 文件存储路径名称
     */
    private static final String NAME = "defaultTdEngine";

    @Override
    public void init() {
        if (queueAndRun == null) {
            queueAndRun = new QueueAndRun(DataHandleConstant.worker, DataHandleConstant.popCount, DataHandleConstant.maxWorker);
            queueAndRun.start(NAME, defaultTdEngineHandleRun);
        }
    }

    @Override
    public void stop() {
        if (queueAndRun != null) {
            queueAndRun.stop();
        }
    }

    @Override
    public void doRun(Object object) throws Exception {
    }

    @Override
    public void doPush(byte[] bytes) throws Exception {
        if (queueAndRun == null) {
            init();
        }
        queueAndRun.push(bytes);
    }
}
